package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.SetMealMapper;
import com.fantastic.model.pojo.zjs.SetMeal;
import com.fantastic.service.zjs.SetMealService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/1 12:44
 * @description: 套餐表service层接口实现类
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    @Resource
    private SetMealMapper mealMapper;

    /**
     * 查询套餐表信息
     *
     * @param setMeal
     * @return
     */
    @Override
    public List<SetMeal> listMeal(SetMeal setMeal) {
        List<SetMeal> setMeals = mealMapper.listMeal(setMeal);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), setMeals);
        for (SetMeal list : setMeals) {
            list.setMealPicture(FilesUtils.getSlashParse(list.getMealPicture()));
        }
        return setMeals;
    }

    /**
     * 查询单个套餐信息
     *
     * @param mealId
     * @return
     */
    @Override
    public SetMeal getMeal(Integer mealId) {
        IsEmptyUtils.objectIsNull(new SelectException(RespStatusInfo.SELECT_NULL), mealMapper.getMeal(mealId));
        SetMeal meal = mealMapper.getMeal(mealId);
        meal.setMealPicture(FilesUtils.getSlashParse(meal.getMealPicture()));
        return mealMapper.getMeal(mealId);
    }

    /**
     * 添加套餐信息
     *
     * @param setMeal
     * @return
     */
    @Override
    public Integer insertMeal(MultipartFile mealFile, SetMeal setMeal) {
        //判断字符串的数据不为空
        IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                setMeal.getMealName(), setMeal.getMealDepict());
        //判断Integer类型不能为空
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                setMeal.getMealChoice(), setMeal.getMealStatus());
        //判断对象类型不能为空
        IsEmptyUtils.objectIsNull(new InsertException(RespStatusInfo.DATA_ERROR), setMeal.getMealPrice());
        //文件处理（不为空并且文件大小不等于0）
        if (mealFile != null && mealFile.getSize() != 0) {
            //判断文件类型是否支持
            MultipartFileUtils.supportType(mealFile.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(mealFile, "套餐");
            setMeal.setMealPicture(relativePath);
        } else {
            throw new InsertException(RespStatusInfo.DATA_ERROR);
        }
        Integer integer = mealMapper.insertMeal(setMeal);
        if (integer == null) {
            throw new InsertException(RespStatusInfo.INSERT_ERROR);
        }
        return setMeal.getMealId();
    }

    /**
     * 修改套餐信息
     *
     * @param setMeal
     * @return
     */
    @Override
    public Integer updateMeal(MultipartFile mealFile, SetMeal setMeal) {
        System.out.println(setMeal.getMealId());
        //判断id不能为空
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR),
                setMeal.getMealId());
        //文件处理（不为空并且文件大小不等于0）
        if (mealFile != null && mealFile.getSize() != 0) {
            //判断文件类型是否支持
            MultipartFileUtils.supportType(mealFile.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(mealFile, "套餐");
            setMeal.setMealPicture(relativePath);
            //拿到原来的套餐图片地址
            SetMeal meal = getMeal(setMeal.getMealId());
            //执行删除
            MultipartFileUtils.deleteFile(meal.getMealPicture());
        } else {
            throw new UpdateException(RespStatusInfo.DATA_ERROR);
        }
        return mealMapper.updateMeal(setMeal);
    }

    /**
     * 删除套餐信息
     *
     * @param mealId
     * @return
     */
    @Override
    public Integer deleteMeal(Integer mealId) {
        //判断id不能为空
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DATA_ERROR),
                mealId);
        return mealMapper.deleteMeal(mealId);
    }
}
